#include "stdafx.h"
//#include "CAN.h"
#include "Frame.h"
#include "Util.h"

extern ofstream log;

Frame::Frame()
{
	mBits = NULL;
	mLength = 0;
}

Frame::~Frame()
{
		delete mBits;
}

bool Frame::getBit(int index)
{
	index = index % mLength;
	if(index<0) {index = mLength+index;}
	return mBits[index];
}

int Frame::getLength()
{
	return mLength;
}

FrameType Frame::getFrameType()
{
	return General;
}

/*
----DATA FRAME
START OF FRAME: 1 bit, d
ARBITRATION FIELD: 11 bit ID (first 7 bits must not be all r); 1 bit RTR, d
CONTROL FIELD: 4 bit DATA LENGTH CODE; 2 bit reserved, d
DATA FIELD: 0 to 8 bytes (0 to 64 bits)
CRC FIELD: 15 bits CRC, 1 bit DELIMITER, r
ACK FIELD: 1 bit SLOT, 1 DELIMITER, r
END OF FRAME: 7 bits, r

ONE DATA FRAME: d (11x)d (4x)dd [0~64x] (15x)r xr rrrrrrr
Total: 44~108 bits
*/
//ID must be between 0 to 1015 (inclusive)
DataFrame::DataFrame(unsigned int ID, unsigned int dataLenCode)
{
	mBits = new bool[DATFRM_LEN];
	unsigned int temp = 0;
	bool CRC_input[DATCRC_INPUT] = {false};
	bool CRC_output[CRC_BODY] = {false};
	
	//START OF FRAME: 1 bit, d
	mBits[0] = false;

	//ARBITRATION FIELD: 11 bit ID (first 7 bits must not be all r); 1 bit RTR, d
	temp = ID%(MAX_ID+1);//ID must be between 0 to 1015 (inclusive)
	for(int i=11;i>=1;i--)
	{
		mBits[i] = (temp%2==1);
		temp = temp/2;
	}
	mBits[12] = false;
	
	//CONTROL FIELD: 2 bit reserved, d; 4 bit DATA LENGTH CODE
	mBits[13] = false;
	mBits[14] = false;
	dataLenCode = dataLenCode%9; //Data Length Code must be between 0 to 8 (inclusive)
	mDATLength = 8*dataLenCode;
	mLength = 44+mDATLength;
	for(int i=18;i>=15;i--)
	{
		mBits[i] = (dataLenCode%2==1);
		dataLenCode = dataLenCode/2;
	}

	//DATA FIELD: 0 to 8 bytes (0 to 64 bits)
	for(int i=19;i<=18+mDATLength;i++)
	{
		mBits[i] = (rand()%2==1);
	}

	//CRC FIELD: 15 bits CRC, 1 bit DELIMITER, r
	for(int i=0;i<=18+mDATLength;i++)
	{
		CRC_input[i] = mBits[i];
	}
	calculateCRC(CRC_input, CRC_output, 34+mDATLength);
	for(int i=19+mDATLength;i<=33+mDATLength;i++) 
	{
		mBits[i] = CRC_output[i-19-mDATLength];
	}
	mBits[34+mDATLength] = true;

	//ACK FIELD: 1 bit SLOT, 1 DELIMITER, r
	mBits[35+mDATLength] = true;
	mBits[36+mDATLength] = true;

	//END OF FRAME: 7 bits, r
	for(int i=0;i<=6;i++)
	{
		mBits[37+mDATLength+i] = true;
	}
}

DataFrame::~DataFrame()
{
}

bool DataFrame::isValid()
{
	return true;
}

/*
START OF FRAME: 1 bit, d
ARBITRATION FIELD: 11 bit ID (first 7 bits must not be all r); 1 bit RTR, d
CONTROL FIELD: 4 bit DATA LENGTH CODE; 2 bit reserved, d
DATA FIELD: 0 to 8 bytes (0 to 64 bits)
CRC FIELD: 15 bits CRC, 1 bit DELIMITER, r
ACK FIELD: 1 bit SLOT, 1 DELIMITER, r
END OF FRAME: 7 bits, r
*/
void DataFrame::print()
{
	log << "Data Frame:" << endl;
	//SOF
	log << mBits[0];
	log << " ";
	//ARB-ID
	for(int i=1;i<=11;i++)
	{
		log << mBits[i];
	}
	log << " ";
	//ARB-RTR
	log << mBits[12];
	log << " ";
	//CON-DATLEN
	for(int i=13;i<=16;i++)
	{
		log << mBits[i];
	}
	log << " ";
	//CON-reserved
	for(int i=17;i<=18;i++)
	{
		log << mBits[i];
	}
	log << " ";
	//DAT
	for(int i=19;i<=18+mDATLength;i++)
	{
		log << mBits[i];
	}
	log << " ";
	//fill remain DAT field with blank
	for(int i=1;i<=64-mDATLength;i++)
	{
		log << " ";
	}
	//CRC-body
	for(int i=19+mDATLength;i<=33+mDATLength;i++)
	{
		if(mBits[i]) {log << "1";}
		else {log << "0";}
		//log << mBits[i];
	}
	log << " ";
	//CRC-delimiter
	log << mBits[34+mDATLength];
	log << " ";
	//ACK-body
	log << mBits[35+mDATLength];
	log << " ";
	//ACK-delimiter
	log << mBits[36+mDATLength];
	log << " ";
	//EOF
	for(int i=37+mDATLength;i<=43+mDATLength;i++)
	{
		log << mBits[i];
	}
	log << endl;

	//a complete frame
	//for(int i=0;i<=mLength-1;i++)
	//{
	//	if(mBits[i]) {log << "1";}
	//	else {log << "0";}
	//}
	//log << endl;
}

FrameType DataFrame::getFrameType()
{
	return Data;
}

/*
----REMOTE FRAME
START OF FRAME: 1 bit, d
ARBITRATION FIELD: 11 bit ID (same as the DATA FRAME); 1 bit RTR, r   
CONTROL FIELD: 4 bit DATA LENGTH CODE (same as the DATA FRAME); 2 bit reserved, d
CRC FIELD: 15 bits CRC, 1 bit DELIMITER, r
ACK FIELD: 1 bit SLOT, 1 DELIMITER, r
END OF FRAME: 7 bits, r

ONE DATA FRAME: d (11x)d (4x)dd (15x)r xr rrrrrrr
Total: 44 bits
*/
RemoteFrame::RemoteFrame(unsigned int ID, unsigned int dataLenCode)
{
	mBits = new bool[RMTFRM_LEN];
	unsigned int temp = 0;
	bool CRC_input[RMTCRC_INPUT] = {false};
	bool CRC_output[CRC_BODY] = {false};
	
	//START OF FRAME: 1 bit, d
	mBits[0] = false;

	//ARBITRATION FIELD: 11 bit ID (first 7 bits must not be all r); 1 bit RTR, r
	temp = ID%(MAX_ID+1);//ID must be between 0 to 1015 (inclusive)
	for(int i=11;i>=1;i--)
	{
		mBits[i] = (temp%2==1);
		temp = temp/2;
	}
	mBits[12] = true;
	
	//CONTROL FIELD: 2 bit reserved, d; 4 bit DATA LENGTH CODE
	//srand(unsigned int(time(NULL)));
	mBits[13] = false;
	mBits[14] = false;
	dataLenCode = dataLenCode%9; //Data Length Code must be between 0 to 8 (inclusive)
	mDATLength = 8*dataLenCode;
	mLength = 44+mDATLength;
	for(int i=18;i>=15;i--)
	{
		mBits[i] = (dataLenCode%2==1);
		dataLenCode = dataLenCode/2;
	}

	//CRC FIELD: 15 bits CRC, 1 bit DELIMITER, r
	for(int i=0;i<=18;i++)
	{
		CRC_input[i] = mBits[i];
	}
	calculateCRC(CRC_input, CRC_output, 34);
	for(int i=19;i<=33;i++) 
	{
		mBits[i] = CRC_output[i-19];
	}
	mBits[34] = true;

	//ACK FIELD: 1 bit SLOT, 1 DELIMITER, r
	mBits[35] = true;
	mBits[36] = true;

	//END OF FRAME: 7 bits, r
	for(int i=0;i<=6;i++)
	{
		mBits[37+i] = true;
	}
}

RemoteFrame::~RemoteFrame()
{
}

bool RemoteFrame::isValid()
{
	return true;
}

/*
----REMOTE FRAME
START OF FRAME: 1 bit, d
ARBITRATION FIELD: 11 bit ID (first 7 bits must not be all r); 1 bit RTR, r   
CONTROL FIELD: 4 bit DATA LENGTH CODE; 2 bit reserved, d
CRC FIELD: 15 bits CRC, 1 bit DELIMITER, r
ACK FIELD: 1 bit SLOT, 1 DELIMITER, r
END OF FRAME: 7 bits, r

ONE DATA FRAME: d (11x)d (4x)dd (15x)r xr rrrrrrr
Total: 44 bits
*/
void RemoteFrame::print()
{
	log << "Remote Frame:" << endl;
	//SOF
	log << mBits[0];
	log << " ";
	//ARB-ID
	for(int i=1;i<=11;i++)
	{
		log << mBits[i];
	}
	log << " ";
	//ARB-RTR
	log << mBits[12];
	log << " ";
	//CON-DATLEN
	for(int i=13;i<=16;i++)
	{
		log << mBits[i];
	}
	log << " ";
	//CON-reserved
	for(int i=17;i<=18;i++)
	{
		log << mBits[i];
	}
	log << " ";
	//fill DAT field with blank
	for(int i=1;i<=64;i++)
	{
		log << " ";
	}
	log << " ";
	//CRC-body
	for(int i=19;i<=33;i++)
	{
		if(mBits[i]) {log << "1";}
		else {log << "0";}
	}
	log << " ";
	//CRC-delimiter
	log << mBits[34];
	log << " ";
	//ACK-body
	log << mBits[35];
	log << " ";
	//ACK-delimiter
	log << mBits[36];
	log << " ";
	//EOF
	for(int i=37;i<=43;i++)
	{
		log << mBits[i];
	}
	log << endl;
}

FrameType RemoteFrame::getFrameType()
{
	return Remote;
}